


import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;


/**
 * 
 * @author Zhang Qiwei
 * These codes implement the coding area for users and the line numbers and marks
 * to indicate the errors when compiling.
 *
 */
public class CodingPane extends JPanel implements DocumentListener {
	TextEditor textPane;
	//static JTextPane lineNumberColumn;
	JScrollPane sp;
    int i;
	int k=0,num;
	int pos=0;
	
	StyleContext m_context;
	DefaultStyledDocument m_doc;//a document which can mark characters in the form of rich text format
    SimpleAttributeSet lightAttr = new SimpleAttributeSet();//A straightforward implementation of MutableAttributeSet using a hash table
    SimpleAttributeSet removeLightAttr = new SimpleAttributeSet();
    
    
    
    /**
     * 
     * The constructor contains all the functions performed and most of 
     * the components, which is available for 
     * further combination.
     */

	public CodingPane() {
		super();
		this.setLayout(new BorderLayout());
		textPane = new TextEditor();
		textPane.setFocusable(true);
		// create the text for user's code.
		
		
		JPanel text=new JPanel(new BorderLayout());
		text.add(textPane,BorderLayout.CENTER);
		
		
		
		
		
		
		
		sp = new JScrollPane(text);

		sp.setRowHeaderView(textPane.lineNumberColumn);//??????????????????????
		// set the scroll panel (lineNumberColumn in the class TextEditor)as the row header of the outside scroll panel.
        
		
		StyleConstants.setBackground(lightAttr, Color.white);//????????????????
	    StyleConstants.setBackground(removeLightAttr, Color.white);
	      //enterAddress[0]=0;
	    m_context = new StyleContext();
	    m_doc = new DefaultStyledDocument(m_context);
		this.add(sp, BorderLayout.CENTER);
		textPane.getDocument().addDocumentListener(this);
		//add listener for the "textArea" to check every update of the text		
		
		
	}

	

	public void changedUpdate(DocumentEvent e) {//this method must be implemented in order to use DocumentListener

	}

	public void insertUpdate(DocumentEvent e) {//this method must be implemented in order to use DocumentListener
	   
	}

	

	public void removeUpdate(DocumentEvent arg0) {//if a portion of the document has been removed, do nothing
		insertUpdate(arg0);

	}

	/**
	 * This method set the cursor position
	 * @param int the line number
	 * */
	public void setCursorPos(int lineNum){//??????????????????????????
		textPane.setCaretPosition(textPane.getEnterOffset().get(lineNum-1));
		
	}

	
	/** 
	 * This method sets the text
	 * @param string
	 * */
	
	public void setText(String s){
		textPane.setText(s);
	}
	
	/**
	 * This class get the text
	 * @return string
	 * */
	public String getText(){
		return textPane.getText();
	}
	
	/**
	 * This method informs whether the text change
	 * @return boolean*/
	public boolean textChange(){
		return textPane.getTextChange();
	}
}
